package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.FeedBackDao;
import com.baichang.taoshunda.entity.Bussiness;
import com.baichang.taoshunda.entity.DeliveryClerk;
import com.baichang.taoshunda.entity.Feedback;
import com.baichang.taoshunda.entity.User;
import com.baichang.taoshunda.service.BusinessService;
import com.baichang.taoshunda.service.DeliveryClerkService;
import com.baichang.taoshunda.service.FeedBackService;
import com.baichang.taoshunda.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class FeedBackServiceImpl implements FeedBackService {

    @Autowired
    private BusinessService businessService;

    @Autowired
    private UserService userService;

    @Autowired
    private FeedBackDao feedBackDao;

    @Autowired
    private DeliveryClerkService deliveryClerkService;

    /**
     * 意见反馈
     *
     * @param bussId  商家编号
     * @param content 内容
     * @return
     */
    @Override
    public boolean feedBack(Integer bussId, String content) throws ServiceException {
        Bussiness bussiness = businessService.getBusinessById(bussId);
        Feedback feedback = new Feedback();
        feedback.setBussId(bussId);
        feedback.setContent(content);
        feedback.setCreated(new Date());
        feedback.setMobile(bussiness.getAccount());
        feedback.setState(1);
        feedback.setStatus(1);
        feedback.setUserName(bussiness.getBussName());
        return feedBackDao.save(feedback) > 0;
    }

    /**
     * 意见反馈
     *
     * @param userId  用户编号
     * @param content 内容
     * @return
     */
    @Override
    public boolean userFeedBack(Integer userId, String content) throws ServiceException {
        User user = userService.getUserBeanById(userId);
        Feedback feedback = new Feedback();
        feedback.setUserId((long) userId);
        feedback.setContent(content);
        feedback.setCreated(new Date());
        feedback.setMobile(user.getAccount());
        feedback.setState(1);
        feedback.setStatus(1);
        feedback.setUserName(user.getNickName());
        return feedBackDao.save(feedback) > 0;
    }

    /**
     * 意见反馈
     *
     * @param disId   配送员编号
     * @param content 内容
     * @return
     */
    @Override
    public boolean deliveryFeedBack(Integer disId, String content) throws ServiceException {
        DeliveryClerk deliveryClerk = deliveryClerkService.getDeliveryClerkById(disId);
        Feedback feedback = new Feedback();
        feedback.setUserId((long) disId);
        feedback.setContent(content);
        feedback.setCreated(new Date());
        feedback.setMobile(deliveryClerk.getAccount());
        feedback.setState(1);
        feedback.setStatus(1);
        feedback.setUserName(deliveryClerk.getName());
        return feedBackDao.save(feedback) > 0;
    }

    /**
     * 查询
     *
     * @param nowPage
     * @param pageSize
     * @param checkUserName
     * @return
     */
    @Override
    public Page queryOpinion(int nowPage, int pageSize, String checkUserName) {
        return feedBackDao.queryOpinion(nowPage, pageSize,checkUserName);
    }
}
